/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Communication;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.Random;

/**
 *
 * @author aaron
 */
public class ConsolePipeReader extends LogReader {
    ConnectionJHaveServerLogGrepper cg;
    VisualizationJHaveServerLogGrepper vg;
    static BufferedReader br = null;
    static String myServerId = null;
    static Random r = new Random(new Date().getTime());

    public static void main(String[] args) throws IOException, InterruptedException {
        if(args.length > 0) {
            myServerId = args[0];
        } else {
            myServerId = Integer.toString(Math.abs(r.nextInt()));
        }
        br = new BufferedReader(new InputStreamReader(System.in));

        ConsolePipeReader p = new ConsolePipeReader(myServerId);

        Thread.sleep(50);

        while(checkShouldContinueReading()) {
            String line = br.readLine();
            p.inputLine(line);
        }

        br.close();
    }

    protected static boolean checkShouldContinueReading() throws IOException, InterruptedException {
        if(br.ready() || System.in.available() > 0) {
            return true;
        } else {
            Thread.sleep(10);
            if(br.ready() || System.in.available() > 0) {
                return true;
            } else {
                return false;
            }
        }
    }

    public ConsolePipeReader(String serverId) {
        super(serverId);
    }
}
